<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>cookies_test.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_net_cookies_test.js.html">cookies_test.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2006 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>goog.provide(&#39;goog.net.cookiesTest&#39;);
<a name="line16"></a>goog.setTestOnly(&#39;goog.net.cookiesTest&#39;);
<a name="line17"></a>
<a name="line18"></a>goog.require(&#39;goog.array&#39;);
<a name="line19"></a>goog.require(&#39;goog.net.cookies&#39;);
<a name="line20"></a>goog.require(&#39;goog.testing.PropertyReplacer&#39;);
<a name="line21"></a>goog.require(&#39;goog.testing.jsunit&#39;);
<a name="line22"></a>
<a name="line23"></a>var cookies = goog.net.cookies;
<a name="line24"></a>var baseCount = 0;
<a name="line25"></a>var stubs = new goog.testing.PropertyReplacer();
<a name="line26"></a>
<a name="line27"></a>function checkForCookies() {
<a name="line28"></a>  if (!cookies.isEnabled()) {
<a name="line29"></a>    var message = &#39;Cookies must be enabled to run this test.&#39;;
<a name="line30"></a>    if (location.protocol == &#39;file:&#39;) {
<a name="line31"></a>      message += &#39;\nNote that cookies for local files are disabled in some &#39; +
<a name="line32"></a>          &#39;browsers.\nThey can be enabled in Chrome with the &#39; +
<a name="line33"></a>          &#39;--enable-file-cookies flag.&#39;;
<a name="line34"></a>    }
<a name="line35"></a>
<a name="line36"></a>    fail(message);
<a name="line37"></a>  }
<a name="line38"></a>}
<a name="line39"></a>
<a name="line40"></a>function setUp() {
<a name="line41"></a>  checkForCookies();
<a name="line42"></a>
<a name="line43"></a>  // Make sure there are no cookies set by previous, bad tests.
<a name="line44"></a>  cookies.clear();
<a name="line45"></a>  baseCount = cookies.getCount();
<a name="line46"></a>}
<a name="line47"></a>
<a name="line48"></a>function tearDown() {
<a name="line49"></a>  // Clear up after ourselves.
<a name="line50"></a>  cookies.clear();
<a name="line51"></a>  stubs.reset();
<a name="line52"></a>}
<a name="line53"></a>
<a name="line54"></a>function testIsEnabled() {
<a name="line55"></a>  assertEquals(navigator.cookieEnabled, cookies.isEnabled());
<a name="line56"></a>}
<a name="line57"></a>
<a name="line58"></a>function testCount() {
<a name="line59"></a>  // setUp empties the cookies
<a name="line60"></a>
<a name="line61"></a>  cookies.set(&#39;testa&#39;, &#39;A&#39;);
<a name="line62"></a>  assertEquals(baseCount + 1, cookies.getCount());
<a name="line63"></a>  cookies.set(&#39;testb&#39;, &#39;B&#39;);
<a name="line64"></a>  cookies.set(&#39;testc&#39;, &#39;C&#39;);
<a name="line65"></a>  assertEquals(baseCount + 3, cookies.getCount());
<a name="line66"></a>  cookies.remove(&#39;testa&#39;);
<a name="line67"></a>  cookies.remove(&#39;testb&#39;);
<a name="line68"></a>  assertEquals(baseCount + 1, cookies.getCount());
<a name="line69"></a>  cookies.remove(&#39;testc&#39;);
<a name="line70"></a>  assertEquals(baseCount + 0, cookies.getCount());
<a name="line71"></a>}
<a name="line72"></a>
<a name="line73"></a>function testSet() {
<a name="line74"></a>  cookies.set(&#39;testa&#39;, &#39;testb&#39;);
<a name="line75"></a>  assertEquals(&#39;testb&#39;, cookies.get(&#39;testa&#39;));
<a name="line76"></a>  cookies.remove(&#39;testa&#39;);
<a name="line77"></a>  assertEquals(undefined, cookies.get(&#39;testa&#39;));
<a name="line78"></a>  // check for invalid characters in name and value
<a name="line79"></a>}
<a name="line80"></a>
<a name="line81"></a>function testGetKeys() {
<a name="line82"></a>  cookies.set(&#39;testa&#39;, &#39;A&#39;);
<a name="line83"></a>  cookies.set(&#39;testb&#39;, &#39;B&#39;);
<a name="line84"></a>  cookies.set(&#39;testc&#39;, &#39;C&#39;);
<a name="line85"></a>  var keys = cookies.getKeys();
<a name="line86"></a>  assertTrue(goog.array.contains(keys, &#39;testa&#39;));
<a name="line87"></a>  assertTrue(goog.array.contains(keys, &#39;testb&#39;));
<a name="line88"></a>  assertTrue(goog.array.contains(keys, &#39;testc&#39;));
<a name="line89"></a>}
<a name="line90"></a>
<a name="line91"></a>
<a name="line92"></a>function testGetValues() {
<a name="line93"></a>  cookies.set(&#39;testa&#39;, &#39;A&#39;);
<a name="line94"></a>  cookies.set(&#39;testb&#39;, &#39;B&#39;);
<a name="line95"></a>  cookies.set(&#39;testc&#39;, &#39;C&#39;);
<a name="line96"></a>  var values = cookies.getValues();
<a name="line97"></a>  assertTrue(goog.array.contains(values, &#39;A&#39;));
<a name="line98"></a>  assertTrue(goog.array.contains(values, &#39;B&#39;));
<a name="line99"></a>  assertTrue(goog.array.contains(values, &#39;C&#39;));
<a name="line100"></a>}
<a name="line101"></a>
<a name="line102"></a>
<a name="line103"></a>function testContainsKey() {
<a name="line104"></a>  assertFalse(cookies.containsKey(&#39;testa&#39;));
<a name="line105"></a>  cookies.set(&#39;testa&#39;, &#39;A&#39;);
<a name="line106"></a>  assertTrue(cookies.containsKey(&#39;testa&#39;));
<a name="line107"></a>  cookies.set(&#39;testb&#39;, &#39;B&#39;);
<a name="line108"></a>  assertTrue(cookies.containsKey(&#39;testb&#39;));
<a name="line109"></a>  cookies.remove(&#39;testb&#39;);
<a name="line110"></a>  assertFalse(cookies.containsKey(&#39;testb&#39;));
<a name="line111"></a>  cookies.remove(&#39;testa&#39;);
<a name="line112"></a>  assertFalse(cookies.containsKey(&#39;testa&#39;));
<a name="line113"></a>}
<a name="line114"></a>
<a name="line115"></a>
<a name="line116"></a>function testContainsValue() {
<a name="line117"></a>  assertFalse(cookies.containsValue(&#39;A&#39;));
<a name="line118"></a>  cookies.set(&#39;testa&#39;, &#39;A&#39;);
<a name="line119"></a>  assertTrue(cookies.containsValue(&#39;A&#39;));
<a name="line120"></a>  cookies.set(&#39;testb&#39;, &#39;B&#39;);
<a name="line121"></a>  assertTrue(cookies.containsValue(&#39;B&#39;));
<a name="line122"></a>  cookies.remove(&#39;testb&#39;);
<a name="line123"></a>  assertFalse(cookies.containsValue(&#39;B&#39;));
<a name="line124"></a>  cookies.remove(&#39;testa&#39;);
<a name="line125"></a>  assertFalse(cookies.containsValue(&#39;A&#39;));
<a name="line126"></a>}
<a name="line127"></a>
<a name="line128"></a>
<a name="line129"></a>function testIsEmpty() {
<a name="line130"></a>  // we cannot guarantee that we have no cookies so testing for the true
<a name="line131"></a>  // case cannot be done without a mock document.cookie
<a name="line132"></a>  cookies.set(&#39;testa&#39;, &#39;A&#39;);
<a name="line133"></a>  assertFalse(cookies.isEmpty());
<a name="line134"></a>  cookies.set(&#39;testb&#39;, &#39;B&#39;);
<a name="line135"></a>  assertFalse(cookies.isEmpty());
<a name="line136"></a>  cookies.remove(&#39;testb&#39;);
<a name="line137"></a>  assertFalse(cookies.isEmpty());
<a name="line138"></a>  cookies.remove(&#39;testa&#39;);
<a name="line139"></a>}
<a name="line140"></a>
<a name="line141"></a>
<a name="line142"></a>function testRemove() {
<a name="line143"></a>  assertFalse(
<a name="line144"></a>      &#39;1. Cookie should not contain &quot;testa&quot;&#39;, cookies.containsKey(&#39;testa&#39;));
<a name="line145"></a>  cookies.set(&#39;testa&#39;, &#39;A&#39;, undefined, &#39;/&#39;);
<a name="line146"></a>  assertTrue(&#39;2. Cookie should contain &quot;testa&quot;&#39;, cookies.containsKey(&#39;testa&#39;));
<a name="line147"></a>  cookies.remove(&#39;testa&#39;, &#39;/&#39;);
<a name="line148"></a>  assertFalse(
<a name="line149"></a>      &#39;3. Cookie should not contain &quot;testa&quot;&#39;, cookies.containsKey(&#39;testa&#39;));
<a name="line150"></a>
<a name="line151"></a>  cookies.set(&#39;testa&#39;, &#39;A&#39;);
<a name="line152"></a>  assertTrue(&#39;4. Cookie should contain &quot;testa&quot;&#39;, cookies.containsKey(&#39;testa&#39;));
<a name="line153"></a>  cookies.remove(&#39;testa&#39;);
<a name="line154"></a>  assertFalse(
<a name="line155"></a>      &#39;5. Cookie should not contain &quot;testa&quot;&#39;, cookies.containsKey(&#39;testa&#39;));
<a name="line156"></a>}
<a name="line157"></a>
<a name="line158"></a>function testStrangeValue() {
<a name="line159"></a>  // This ensures that the pattern key2=value in the value does not match
<a name="line160"></a>  // the key2 cookie.
<a name="line161"></a>  var value = &#39;testb=bbb&#39;;
<a name="line162"></a>  var value2 = &#39;ccc&#39;;
<a name="line163"></a>
<a name="line164"></a>  cookies.set(&#39;testa&#39;, value);
<a name="line165"></a>  cookies.set(&#39;testb&#39;, value2);
<a name="line166"></a>
<a name="line167"></a>  assertEquals(value, cookies.get(&#39;testa&#39;));
<a name="line168"></a>  assertEquals(value2, cookies.get(&#39;testb&#39;));
<a name="line169"></a>}
<a name="line170"></a>
<a name="line171"></a>function testSetCookiePath() {
<a name="line172"></a>  assertEquals(&#39;foo=bar;path=/xyz&#39;,
<a name="line173"></a>      mockSetCookie(&#39;foo&#39;, &#39;bar&#39;, -1, &#39;/xyz&#39;));
<a name="line174"></a>}
<a name="line175"></a>
<a name="line176"></a>function testSetCookieDomain() {
<a name="line177"></a>  assertEquals(&#39;foo=bar;domain=google.com&#39;,
<a name="line178"></a>      mockSetCookie(&#39;foo&#39;, &#39;bar&#39;, -1, null, &#39;google.com&#39;));
<a name="line179"></a>}
<a name="line180"></a>
<a name="line181"></a>function testSetCookieSecure() {
<a name="line182"></a>  assertEquals(&#39;foo=bar;secure&#39;,
<a name="line183"></a>      mockSetCookie(&#39;foo&#39;, &#39;bar&#39;, -1, null, null, true));
<a name="line184"></a>}
<a name="line185"></a>
<a name="line186"></a>function testSetCookieMaxAgeZero() {
<a name="line187"></a>  var result = mockSetCookie(&#39;foo&#39;, &#39;bar&#39;, 0);
<a name="line188"></a>  var pattern = new RegExp(
<a name="line189"></a>      &#39;foo=bar;expires=&#39; + new Date(1970, 1, 1).toUTCString());
<a name="line190"></a>  if (!result.match(pattern)) {
<a name="line191"></a>    fail(&#39;expected match against &#39; + pattern + &#39; got &#39; + result);
<a name="line192"></a>  }
<a name="line193"></a>}
<a name="line194"></a>
<a name="line195"></a>function testGetEmptyCookie() {
<a name="line196"></a>  var value = &#39;&#39;;
<a name="line197"></a>
<a name="line198"></a>  cookies.set(&#39;test&#39;, value);
<a name="line199"></a>
<a name="line200"></a>  assertEquals(value, cookies.get(&#39;test&#39;));
<a name="line201"></a>}
<a name="line202"></a>
<a name="line203"></a>function testGetEmptyCookieIE() {
<a name="line204"></a>  stubs.set(cookies, &#39;getCookie_&#39;, function() {
<a name="line205"></a>    return &#39;test1; test2; test3&#39;;
<a name="line206"></a>  });
<a name="line207"></a>
<a name="line208"></a>  assertEquals(&#39;&#39;, cookies.get(&#39;test1&#39;));
<a name="line209"></a>  assertEquals(&#39;&#39;, cookies.get(&#39;test2&#39;));
<a name="line210"></a>  assertEquals(&#39;&#39;, cookies.get(&#39;test3&#39;));
<a name="line211"></a>}
<a name="line212"></a>
<a name="line213"></a>// TODO(chrisn): Testing max age &gt; 0 requires a mock clock.
<a name="line214"></a>
<a name="line215"></a>function mockSetCookie(var_args) {
<a name="line216"></a>  var setCookie = cookies.setCookie_;
<a name="line217"></a>  try {
<a name="line218"></a>    var result;
<a name="line219"></a>    cookies.setCookie_ = function(arg) {
<a name="line220"></a>      result = arg;
<a name="line221"></a>    };
<a name="line222"></a>    cookies.set.apply(cookies, arguments);
<a name="line223"></a>    return result;
<a name="line224"></a>  } finally {
<a name="line225"></a>    cookies.setCookie_ = setCookie;
<a name="line226"></a>  }
<a name="line227"></a>}
<a name="line228"></a>
<a name="line229"></a>function assertValidName(name) {
<a name="line230"></a>  assertTrue(name + &#39; should be valid&#39;, cookies.isValidName(name));
<a name="line231"></a>}
<a name="line232"></a>
<a name="line233"></a>function assertInvalidName(name) {
<a name="line234"></a>  assertFalse(name + &#39; should be invalid&#39;, cookies.isValidName(name));
<a name="line235"></a>  assertThrows(function() {
<a name="line236"></a>    cookies.set(name, &#39;value&#39;);
<a name="line237"></a>  });
<a name="line238"></a>}
<a name="line239"></a>
<a name="line240"></a>function assertValidValue(val) {
<a name="line241"></a>  assertTrue(val + &#39; should be valid&#39;, cookies.isValidValue(val));
<a name="line242"></a>}
<a name="line243"></a>
<a name="line244"></a>function assertInvalidValue(val) {
<a name="line245"></a>  assertFalse(val + &#39; should be invalid&#39;, cookies.isValidValue(val));
<a name="line246"></a>  assertThrows(function() {
<a name="line247"></a>    cookies.set(&#39;name&#39;, val);
<a name="line248"></a>  });
<a name="line249"></a>}
<a name="line250"></a>
<a name="line251"></a>function testValidName() {
<a name="line252"></a>  assertValidName(&#39;foo&#39;);
<a name="line253"></a>  assertInvalidName(&#39;foo bar&#39;);
<a name="line254"></a>  assertInvalidName(&#39;foo=bar&#39;);
<a name="line255"></a>  assertInvalidName(&#39;foo;bar&#39;);
<a name="line256"></a>  assertInvalidName(&#39;foo\nbar&#39;);
<a name="line257"></a>}
<a name="line258"></a>
<a name="line259"></a>function testValidValue() {
<a name="line260"></a>  assertValidValue(&#39;foo&#39;);
<a name="line261"></a>  assertValidValue(&#39;foo bar&#39;);
<a name="line262"></a>  assertValidValue(&#39;foo=bar&#39;);
<a name="line263"></a>  assertInvalidValue(&#39;foo;bar&#39;);
<a name="line264"></a>  assertInvalidValue(&#39;foo\nbar&#39;);
<a name="line265"></a>}
</pre>


</body>
</html>
